package com.lihui.dao;

import com.lihui.model.Classes;
import com.lihui.model.Course;
import com.lihui.util.DBUtil;
import com.sun.org.apache.bcel.internal.generic.ACONST_NULL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 根据班级名称查班级信息
 * @author wileda
 * @date 2022/12/28  17:08
 */
public class ClassesDao {
    public Classes selectByName(String classesName){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
//        Classes result = null;
        //连接数据库
        try {
            connection = DBUtil.getConnection();
            //构造sql语句
            String sql = "select id, name, 'desc' from classes where name = ?";
            //获取预处理dui象
            statement = connection.prepareStatement(sql);
            //替换真实值
            statement.setString(1,classesName);
            //执行sql获取返回结果
            resultSet = statement.executeQuery();
            //处理返回结果
            Classes result = null;
            if(resultSet.next()){
                 if(result == null){
                     result = new Classes();
                 }
                 //设置属性的值
                 result.setId(resultSet.getInt(1));
                 result.setName(resultSet.getString(2));
                 result.setDesc(resultSet.getString(3));
            }
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet,statement,connection);
        }

        //返回结果
        return null;
    }



}
